<?php

namespace App\Hospital\Controllers;

use App\Hospital\Models\FamilyModel;
use App\Hospital\Repositories\Family;
use App\Models\Area;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class FamilyController extends AdminController
{
    public function title(){

        $type = request('type');
        if($type){
            return $this->title = FamilyModel::$typeTxt[$type]."签约";
        }else{
            return parent::title();
        }
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Family('sign'), function (Grid $grid) {
            $grid->model()->where('hospital_id',Admin::user()->hospital_id);
            $grid->column('name');
            $grid->column('idcard_type')->using(FamilyModel::$idCardTypeTxt);
            $grid->column('idcard')->display(function($value){
                return substr_replace($value, '********', 6, 8);
            });
            $grid->column('birthday');
            $grid->column('phone','手机号')->display(function($value){
                return substr_replace($value, '****', 3, 4);
            });
            $grid->column('address')->display(function($value){

                $area = Area::query()->whereIn('id',[$this->province,$this->city,$this->county])->pluck('title');
                return implode(',',$area->toArray()).$value;
            });
            $grid->column('first_sign_date','首次签约日期')->display(function($value){

                return date('Y-m-d',strtotime($this->sign->created_at));
            });
            $grid->column('sign_date','最近签约日期')->display(function($value){

                return $this->sign->starttime;
            });

            //$grid->column('sign_date','签约时间');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->equal('type');
            });

            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Family(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('idcard_type');
            $show->field('idcard');
            $show->field('type');
            $show->field('birthday');
            $show->field('phone');
            $show->field('gender');
            $show->field('relation');
            $show->field('province');
            $show->field('county');
            $show->field('city');
            $show->field('address');
            $show->field('avatar');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Family(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('name');
            $form->text('idcard_type');
            $form->text('idcard');
            $form->text('type');
            $form->text('birthday');
            $form->text('phone');
            $form->text('gender');
            $form->text('relation');
            $form->text('province');
            $form->text('county');
            $form->text('city');
            $form->text('address');
            $form->text('avatar');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
